/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.jit.core.load;

import com.inspur.edp.jit.core.common.DirectoryManager;
import com.inspur.edp.jit.core.compile.BytesJavaFileObject;
import com.inspur.edp.jit.core.util.JarUtil;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public class ClassLoadManager {

    private static final ConcurrentMap<String, ConcurrentMap<String, JitClassLoader>> classLoaderMap = new ConcurrentHashMap<>();

    public static void put(String functionType, String functionId, List<BytesJavaFileObject> javaFileObjects) {
        Map<String, byte[]> classBytesMap = javaFileObjects.stream().collect(Collectors
                .toConcurrentMap(BytesJavaFileObject::getClassName, BytesJavaFileObject::getClassBytes));

        ConcurrentMap<String, JitClassLoader> functionTypeMap = classLoaderMap
                .computeIfAbsent(functionType, key -> new ConcurrentHashMap<>());
        functionTypeMap.put(functionId, new JitClassLoader(classBytesMap));
    }

    public static Class<?> load(String functionType, String functionId, String className) {
        classLoaderMap.computeIfAbsent(functionType, key -> new ConcurrentHashMap<>());
        JitClassLoader classLoader = classLoaderMap.get(functionType).computeIfAbsent(functionId, key -> {
            Map<String, byte[]> classBytes = null;
            try {
                classBytes = getClassBytesFromCache(functionType, functionId);
            } catch (FileNotFoundException e) {
                throw new JitClassNotFoundException(functionType, functionId, className, e);
            }
            return new JitClassLoader(classBytes);
        });

        try {
            return classLoader.loadClass(className);
        } catch (ClassNotFoundException e) {
            throw new JitClassNotFoundException(functionType, functionId, className, e);
        }
    }

    private static Map<String, byte[]> getClassBytesFromCache(String functionType, String functionId) throws FileNotFoundException {
        String jitDirectory = DirectoryManager.getJitDirectory(functionType, functionId);
        return JarUtil.read(jitDirectory, functionId);
    }

    public static void unLoad(String functionType, String functionId) {
        if (classLoaderMap.containsKey(functionType)) {
            classLoaderMap.get(functionType).remove(functionId);
        }
    }

}
